package org.lim.interfaces.controller;

import lombok.extern.slf4j.Slf4j;
import org.lim.infrastructure.annotation.AuthCheck;
import org.lim.infrastructure.common.BaseResponse;
import org.lim.infrastructure.common.ResultUtils;
import org.lim.domain.user.valueobject.UserConstant;
import org.lim.infrastructure.exception.BusinessException;
import org.lim.infrastructure.exception.ErrorCode;

import org.lim.interfaces.dto.picture.TagCategoryAddRequest;
import org.lim.domain.picture.entity.PictureCategory;
import org.lim.domain.picture.entity.PictureTag;
import org.lim.interfaces.vo.picture.PictureTagCategory;
import org.lim.application.service.PictureCategoryApplicationService;
import org.lim.application.service.PictureTagApplicationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 图片标签分类控制器
 */
@RestController
@Slf4j
@RequestMapping("/tag_category")
public class TagCategoryController {

    @Resource
    private PictureTagApplicationService pictureTagApplicationService;

    @Resource
    private PictureCategoryApplicationService pictureCategoryApplicationService;

    /**
     * 获取图片标签和分类列表
     */
    @GetMapping("/list")
    public BaseResponse<PictureTagCategory> listPictureTagCategory() {
        PictureTagCategory pictureTagCategory = new PictureTagCategory();

        // 从数据库获取启用的标签列表
        List<PictureTag> tagEntityList = pictureTagApplicationService.getActiveTagList();
        List<String> tagList = tagEntityList.stream()
                .map(PictureTag::getTagName)
                .toList();

        // 从数据库获取启用的分类列表
        List<PictureCategory> categoryEntityList = pictureCategoryApplicationService.getActiveCategoryList();
        List<String> categoryList = categoryEntityList.stream()
                .map(PictureCategory::getCategoryName)
                .toList();

        pictureTagCategory.setTagList(tagList);
        pictureTagCategory.setCategoryList(categoryList);

        return ResultUtils.success(pictureTagCategory);
    }

    /**
     * 批量添加标签（仅管理员可用）
     */
    @PostMapping("/tag/batch_add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> batchAddTags(@RequestBody TagCategoryAddRequest addRequest) {
        if (addRequest == null || addRequest.getNames() == null || addRequest.getNames().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        boolean result = pictureTagApplicationService.batchAddTags(addRequest.getNames());
        return ResultUtils.success(result);
    }

    /**
     * 批量添加分类（仅管理员可用）
     */
    @PostMapping("/category/batch_add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> batchAddCategories(@RequestBody TagCategoryAddRequest addRequest) {
        if (addRequest == null || addRequest.getNames() == null || addRequest.getNames().isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        boolean result = pictureCategoryApplicationService.batchAddCategories(addRequest.getNames());
        return ResultUtils.success(result);
    }

    /**
     * 获取所有标签列表（仅管理员可用）
     */
    @GetMapping("/tag/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<PictureTag>> listAllTags() {
        List<PictureTag> tagList = pictureTagApplicationService.list();
        return ResultUtils.success(tagList);
    }

    /**
     * 获取所有分类列表（仅管理员可用）
     */
    @GetMapping("/category/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<PictureCategory>> listAllCategories() {
        List<PictureCategory> categoryList = pictureCategoryApplicationService.list();
        return ResultUtils.success(categoryList);
    }

    /**
     * 更新标签状态（仅管理员可用）
     */
    @PostMapping("/tag/update_status")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateTagStatus(@RequestParam Long id, @RequestParam Integer isActive) {
        if (id == null || id <= 0 || isActive == null || (isActive != 0 && isActive != 1)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        PictureTag tag = new PictureTag();
        tag.setId(id);
        tag.setIsActive(isActive);

        boolean result = pictureTagApplicationService.updateById(tag);
        return ResultUtils.success(result);
    }

    /**
     * 更新分类状态（仅管理员可用）
     */
    @PostMapping("/category/update_status")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateCategoryStatus(@RequestParam Long id, @RequestParam Integer isActive) {
        if (id == null || id <= 0 || isActive == null || (isActive != 0 && isActive != 1)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        PictureCategory category = new PictureCategory();
        category.setId(id);
        category.setIsActive(isActive);

        boolean result = pictureCategoryApplicationService.updateById(category);
        return ResultUtils.success(result);
    }
}